<?php

/*
 * B端操作功能模块
 * 功能：理发师
 */

namespace Home\Controller;

use Think\B_Controller;

class BarberController extends B_Controller { 
    
    public function __construct() {
        parent::__construct();
    }
    
    public function index(){
        $b = D('Barber','Logic');
        $store_id = (int)$_GET['store_id'];
        if(!in_array($store_id,$this->store_id)){//判断是否有权限查看其它店员
            $store_id = $this->store_id;
        }
        $Barbers = $b->getBarberByStoreIds($store_id);
        $s = D('Store','Logic');
        foreach($Barbers as $k=>$v){
            $store = $s->getStore($v['store_id']);
            $Barbers[$k]['store_name'] = $store['name'];
        }
        $this->assign('store_id',$store_id);
        $this->assign('is_admin',$this->user['is_admin']);
        $this->assign('Barbers',$Barbers);
        $this->display('list');
    }
    
    public function create(){
        if(empty($this->store)){
            redirect('/home/store/');
        }
        
        $b = D('Barber','Logic');
        if($_GET['id']>0){
            $barber_id = $_GET['id'];
            $barber = $b->getBarber($barber_id);
            $this->assign('barber',$barber);
        }
        $level = $b->level();
        $this->assign('level',$level);
        $this->display('create');      
    }
    
    public function submit(){
        $data['store_id']= (int)$_POST['store'];
        $data['name']= $_POST['name'];
        $data['sex']= (int)$_POST['sex'];
        $data['level']= $_POST['level'];
        $data['mobile'] = $_POST['mobile'];
        $data['create_time']= time();
        $data['photo'] = trim($_POST['photo']);
        if(strlen($data['name'])<1
                || strlen($data['mobile'])<1
                || strlen($data['level'])<1){
            $this->_msg('_SYSTEM_ERROR_');
        }

        if(!is_mobile($data['mobile'])){
            redirect('/error/?msg=1');
        }
        
        if(!in_array($data['store_id'],$this->store_id)){
            $this->_msg('_HAVE_NO_LEGAL_POWER_');
        }
        $b = D('Barber','Logic');
        $barber = $b->getBarberByMobile($data['mobile']);
        if($barber['id']>0){
            $this->assign('msg',L('_BARBER_ALREADY_HAVE_'));
            $this->display('msg');
            exit;
        }
        $barber_id = $b->insterBarber($data);
        if(!$barber_id ){
            $this->_msg('_SYSTEM_ERROR_');
        }else{
            redirect('/home/barber');
        }
        
    }
    
    public function update(){
        $data['id']=(int)$_POST['id'];
        $data['store_id'] = (int)$_POST['store'];
        $b = D('Barber','Logic');
        $info = $b->getBarber($data['id']);
        if($data['id']<1){
            $this->_msg('_HAVE_NO_LEGAL_POWER_');
        }
        if(!in_array($info['store_id'],$this->store_id) || !in_array($data['store_id'],$this->store_id)){
            $this->_msg('_HAVE_NO_LEGAL_POWER_');
        }
        $data['name']= $_POST['name'];
        $data['sex']= (int)$_POST['sex'];
        $data['level']= $_POST['level'];
        $data['photo'] = trim($_POST['photo']);
        if(strlen($data['name'])<1
                || $data['store_id']<0
                || strlen($data['level'])<1){
            $this->_msg('_SYSTEM_ERROR_');
        }
        $barber_id = $b->updateBarber($data);
        
            redirect('/home/barber');

    }
    
    public function delete(){
        $data['id']=$_GET['id'];
        $b = D('Barber','Logic');
        $info = $b->getBarber($data['id']);
        if($data['id']<1){
            $this->_msg('_HAVE_NO_LEGAL_POWER_');
        }
        if(!in_array($info['store_id'],$this->store_id)){
            $this->_msg('_HAVE_NO_LEGAL_POWER_');
        }
        if(($b->updateBarberStatus($data['id'],$status=0))==false){
            $this->_msg('_SYSTEM_ERROR_');
        }
        redirect('/home/barber');

    }
    
    public function img(){
        layout(false);
        $this->display('img');
    }
    
    public function upload(){ 
        layout(false);
        import('Think.upload');
        $u = new \Think\Upload();
        $path = $u->upload($_FILES);
        $array = array_keys($path);
        if($array[0]=='code'){
            $this->assign('path','/public/upload/'.$path['code']['savepath'].$path['code']['savename']);
            $this->display('code');
        }else{
            $this->assign('path','/public/upload/'.$path['img']['savepath'].$path['img']['savename']);
            $this->display('img');
        }
    }
    
}